/* ************************************************************************** *
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.
 * 
 * This file is licensed to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * ************************************************************************** */
package org.statewalker.fsm;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * This is a composite process listener used to dispatch method calls to
 * multiple listeners.
 * 
 * @author kotelnikov
 */
public class CompositeFsmStateListener<S, E> implements IFsmStateListener<S, E> {

    /**
     * The internal sets containing listeners.
     */
    private Set<IFsmStateListener<S, E>> fSet = new LinkedHashSet<IFsmStateListener<S, E>>();

    /**
     * The default constructor.
     */
    public CompositeFsmStateListener() {
        super();
    }

    /**
     * Adds a new listener to the internal set of listeners.
     * 
     * @param listener the listener to add.
     */
    @SuppressWarnings("unchecked")
    public synchronized void addListener(IFsmStateListener<S, E> listener) {
        addListeners(listener);
    }

    /**
     * Adds all listeners from the specified array.
     * 
     * @param listeners array of listeners to add.
     */
    public synchronized void addListeners(IFsmStateListener<S, E>... listeners) {
        LinkedHashSet<IFsmStateListener<S, E>> set = new LinkedHashSet<IFsmStateListener<S, E>>(
            fSet);
        boolean ok = false;
        for (IFsmStateListener<S, E> listener : listeners) {
            ok |= set.add(listener);
        }
        if (ok) {
            fSet = set;
        }
    }

    /**
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (!(obj instanceof CompositeFsmStateListener<?, ?>))
            return false;
        CompositeFsmStateListener<?, ?> o = (CompositeFsmStateListener<?, ?>) obj;
        return fSet.equals(o.fSet);
    }

    /**
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return fSet.hashCode();
    }

    /**
     * @see org.statewalker.fsm.IFsmStateListener#onActivate(java.lang.Object,
     *      java.lang.Object, java.lang.Object)
     */
    public void onActivate(S parent, S state, E event) {
        Set<IFsmStateListener<S, E>> set = fSet;
        for (IFsmStateListener<S, E> listener : set) {
            listener.onActivate(parent, state, event);
        }
    }

    /**
     * @see org.statewalker.fsm.IFsmStateListener#onDeactivate(java.lang.Object,
     *      java.lang.Object, java.lang.Object)
     */
    public void onDeactivate(S parent, S state, E event) {
        Set<IFsmStateListener<S, E>> set = fSet;
        for (IFsmStateListener<S, E> listener : set) {
            listener.onDeactivate(parent, state, event);
        }
    }

    /**
     * @see org.statewalker.fsm.IFsmStateListener#onTransition(java.lang.Object,
     *      java.lang.Object, java.lang.Object, java.lang.Object)
     */
    public void onTransition(S parent, S prev, E event, S next) {
        Set<IFsmStateListener<S, E>> set = fSet;
        for (IFsmStateListener<S, E> listener : set) {
            listener.onTransition(parent, prev, event, next);
        }
    }

    /**
     * Removes the specified listener from the internal collection of listeners.
     * 
     * @param listener the listener to remove
     */
    public synchronized void removeListener(IFsmStateListener<S, E> listener) {
        LinkedHashSet<IFsmStateListener<S, E>> set = new LinkedHashSet<IFsmStateListener<S, E>>(
            fSet);
        boolean ok = set.remove(listener);
        if (ok) {
            fSet = set;
        }
    }

    /**
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return fSet.toString();
    }

}
